package com.winit.openapi.wop.inbound.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.mms.spi.v2.item.ItemService;
import com.winit.mms.spi.v2.item.command.QueryItemsForOrderCommand;
import com.winit.mms.spi.v2.item.vo.ItemVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
import com.winit.openapi.wop.inbound.constant.WHInboundConstant;
import com.winit.pms.spi.winit.WinitProductService;
import com.winit.pms.spi.winit.command.WinitProductQueryCommand;
import com.winit.pms.spi.winit.vo.WinitProduct;
import com.winit.ums.spi.UmsCustomerService;
import com.winit.ums.spi.commnad.customer.QueryCompanylCommand;
import com.winit.ums.spi.common.command.GetByCodeCommand;
import com.winit.ums.spi.vo.CustomerCompanyVo;
import com.winit.ums.spi.vo.CustomerInfoVO;
import com.winit.ups.spi.command.CodeCommand;
import com.winit.ups.spi.warehouse.WarehouseService;
import com.winit.ups.spi.warehouse.vo.WarehouseVo;

@Controller
@RequestMapping(value = "/winit/wh/mms")
public class WHInboundMerchandiseController extends BaseController {

    /**
     * 商品服务
     */
    @Resource
    private ItemService        itemService;

    /**
     * ums客户信息
     */
    @Resource
    private UmsCustomerService umsCustomerService;
    

    @Resource
    private WarehouseService       upsWarehouseService;
    
    @Resource
    private WinitProductService     winitProductService;


    /**
     * 根据客户编码，头程类型，winit产品编码，仓库CODE查询商品信息
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/getAvailableMerchandise")
    public String getAvailableMerchandise(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        JSONObject json = null;
        if (obj instanceof JSONObject) {
            json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "customerCode");
            ValidateUtil.validNotNull(json, "firstLegType");
            ValidateUtil.validNotNull(json, "winitProductCode");
            ValidateUtil.validNotNull(json, "destinationWarehouseCode");
            ValidateUtil.validMaxLength(json, "customerCode", 50, true);
            ValidateUtil.validMaxLength(json, "firstLegType", 20, true);
            ValidateUtil.validMaxLength(json, "winitProductCode", 50, true);
            ValidateUtil.validMaxLength(json, "destinationWarehouseCode", 50, true);
            
            if (json.containsKey("customerCode")) {
                String customerCode = json.getString("customerCode");
                GetByCodeCommand queryCommand = new GetByCodeCommand();
                queryCommand.setCode(customerCode);
                CustomerCompanyVo vo = umsCustomerService.queryCompanyByCode(queryCommand);
                if (null == vo) {
                    throw new ApiException(ErrorCode.USER_NOT_FOUND, customerCode);
                }
            }
            
            if (json.containsKey("winitProductCode")) {
                String winitProductCode = json.getString("winitProductCode");
                WinitProductQueryCommand productCommand = new WinitProductQueryCommand();
                productCommand.setCode(winitProductCode);
                WinitProduct product = winitProductService.queryProduct(productCommand);
                if (null == product) {
                    throw new ApiException(ErrorCode.WINIT_PRODUCT_NOT_EXISTS, winitProductCode);
                }
            }
            
            if (json.containsKey("destinationWarehouseCode")) {
                CodeCommand warehouseCommand = new CodeCommand();
                warehouseCommand.setCode(json.getString("destinationWarehouseCode"));
                WarehouseVo warehouse = upsWarehouseService.queryWarehouseByCode(warehouseCommand);
                if (null == warehouse) {
                    throw new ApiException(ErrorCode.WAREHOUSE_CODE_NOT_EXISTS,
                        json.getString("destinationWarehouseCode"));
                }
            }
            
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        if (!WHInboundConstant.FirstLegType.isValidOrderType(json.getString("firstLegType"))) {
            throw new ApiException(ErrorCode.FEILD_NOT_MATCH, "firstLegType");
        }
        QueryItemsForOrderCommand command=new QueryItemsForOrderCommand();
        command.setCtx(CommandContext.getContext());
        command.setCustomerCode(json.getString("customerCode"));
        command.setFirstLegType(json.getString("firstLegType"));
        command.setProductCode(json.getString("winitProductCode"));
        command.setWarehouseCode(json.getString("destinationWarehouseCode"));
        
        QueryCompanylCommand queryCompanylCommand = new QueryCompanylCommand();
        queryCompanylCommand.setEmail(command.getCtx().getUsername());
        CustomerInfoVO customerInfoVO = umsCustomerService.queryCompanyByUserName(queryCompanylCommand);
        if (customerInfoVO == null) {
            throw new ApiException(ErrorCode.USER_NOT_FOUND);
        }
        command.setCustomerCode(customerInfoVO.getCode());

        List<ItemVo> itemVos = itemService.queryItemsForOrder(command);
        List<Map<String, String>> itemList = new ArrayList<Map<String, String>>();
        if(CollectionUtils.isNotEmpty(itemVos)){
            for (ItemVo itemVo : itemVos) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("winitMerchandiseCode", itemVo.getCode());
                map.put("merchandiseCode", itemVo.getSkuCode());
                map.put("merchandiseName", itemVo.getName());
                map.put("merchandiseNameCn", itemVo.getCnName());
                map.put("specification", itemVo.getSpecification());
                itemList.add(map);
            }
            
        }
        
        responseMsg.setData(itemList);
        return SUCCESS;
    }

}
